package com.wyp168.leetcode.treenode;

import com.wyp168.leetcode.listnode.ListNode;

/**
 * @ClassName IsSameTree
 * @Description TODO
 * @Author wyp168
 * @Date 2022/8/20 11:20
 */
public class IsSameTree {
    public static void main(String[] args) {
        TreeNode p = new TreeNode(1);
        TreeNode pp = new TreeNode(2);
        p.left = pp;

        TreeNode q = new TreeNode(1);
        TreeNode qqq = new TreeNode(2);
        q.right = qqq;

        IsSameTree isSameTree = new IsSameTree();
        System.out.println(isSameTree.isSameTree(p, q));
    }

    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        } else if ( (p == null && q != null) || (p != null && q == null) ) {
            return false;
        }
        if (p.val != q.val) {
            return false;
        }
        boolean isSameLeft = isSameTree(p.left, q.left);
        boolean isSameRight = isSameTree(p.right, q.right);
        return isSameLeft & isSameRight;
    }
}
